首页 > 试题广场 >

找出字符串中第一个只出现一次的字符

[编程题]找出字符串中第一个只出现一次的字符
  • 热度指数:199596 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
找出字符串中第一个只出现一次的字符


数据范围:输入的字符串长度满足



输入描述:

输入一个非空字符串



输出描述:

输出第一个只出现一次的字符,如果不存在输出-1

示例1

输入

asdfasdfo

输出

o
没用函数count,笨蛋写法
while True:
    try:
        data = list(input())
        res = []
        dic = {}
        for i in data:
            if i not in res:
                res.append(i)
                dic[i] = 1
            else:
                dic[i] += 1
        if 1 in dic.values():
            print(list(dic.keys())[list(dic.values()).index(1)])
        else:
            print(-1)
    except:
        break

发表于 2023-02-22 14:59:40 回复(0)
line = input()

count = dict()
for letter in line:
    if count.get(letter):
        count[letter] += 1
    else:
        count[letter] = 1

for key in count:
    if count[key] > 1:
        line = line.replace(key,"")

if len(line) == 0:
    print(-1)
else:
    print(line[0])

发表于 2023-02-15 23:10:53 回复(0)
n = input()
l = []
for i in n:
    if n.count(i) == 1:
        l.append(i)

if len(l) == 0:
    print(-1)
else:
    print(l[0])

发表于 2022-09-16 17:15:35 回复(0)
s = input()
for i in s:
    if s.count(i) == 1:
        print(i)
        break
else:
    print('-1')
发表于 2022-09-10 07:43:28 回复(0)
ss = input()
result = ""
for i in range(len(ss)):
    if i < len(ss):
        if ss[i] not in ss[i+1:] and ss[i] not in ss[:i]:
            result=ss[i]
            break
    elif i == len(ss):
        if ss[i] not in ss[:i]:
            result=ss[i]
#     print(result,"result")
if result != "":
    print(result)
else:
    print(-1)
发表于 2022-09-07 19:29:38 回复(0)
# 奇怪,自测运行居然会多打印一个-1,搞不懂
while True:
    try:
        s = input()
        ls = []
        for i in list(s):
            ls.append(list(s).count(i))
            if list(s).count(i) == 1:
                print(i)
                break
        if 1 not in ls:
            print(-1)
    except:
        break
发表于 2022-08-28 18:00:22 回复(0)
a = input()
dic = {}
for i in a:
    if i not in dic:
        dic[i] = a.count(i)
        if dic[i] == 1:
            break
if dic[i] == 1:
    print(i)
else:
    print(-1)

发表于 2022-08-27 20:40:13 回复(0)
s=input()
def frist(s):
    for i in s:
        if s.count(i)==1:
            return i
if frist(s):
    print(frist(s))
else:
    print(-1)
发表于 2022-08-10 18:58:52 回复(0)
a=input()
for one in a:
    if a.count(one)==1:
        print(one)
        break
else:
    print(-1)


发表于 2022-08-10 15:19:49 回复(0)
def first_str(n):
    for i in n:
        if n.count(i)==1:
            return i
    return -1
print(first_str(input()))

发表于 2022-08-05 19:06:11 回复(0)
while True:
    try:
        str_in = input()
        # 记录只出现一次的字符有哪些
        str2num = {}
        lst = []
        for s in str_in:
            if s not in list(str2num.keys()):
                str2num[s] = 0
            str2num[s]+=1
            if s not in lst:
                lst.append(s)
        if not min(list(str2num.values()))==1:
            print(-1)
        else:
            for s in lst:
                if str2num[s] == 1:
                    print(s)
                    break
            break
        # 打印第一个只出现一次的字符,否则打印-1
    except:
        break

首先确定哪些元素是只出现一次的,并记录每个第一个出现的元素。于是映射就可以了。即判断,第一个出现的元素是不是只出现了一次?
发表于 2022-07-28 15:50:58 回复(0)
python非count()解法
while True:
    try:
        a = input()
        b = {}
        d = set()
        for i in range(len(a)):
            if a[i] not in d:
                if a[i] not in b:
                    b[a[i]] = [i]
                else:
                    del b[a[i]]
                    d.add(a[i])
        e = []
        for j in b.values():
            e.extend(j)
        if e:
            e.sort()
            print(a[e[0]])
        else:
            print('-1')
    except:
        break


发表于 2022-07-20 22:49:23 回复(0)
python3
from collections import Counter
while True:
    try:
        words = input()
        length = len(words)
        words_dict = Counter(words)
        if 1 in words_dict.values():
            temp_list = []
            for i in words_dict.keys():
                if words_dict[i] == 1:
                    temp_list.append(i)
            # temp_dict = {}
            # for i in range(length):
            #     if words[i] in temp_list:
            #         temp_dict[i] = words[i]
            # print(temp_dict[min(temp_dict.keys())])
            for i in range(length):
                if words[i] in temp_list:
                    print(words[i])
                    break
        else:
            print(-1)
    except:
        break


发表于 2022-07-18 17:46:28 回复(0)
a = input()
for i in a:
    if a.count(i)==1:
        print(i)
        break
else:
    print(-1)

发表于 2022-07-13 00:12:08 回复(0)
my_str = [i for i in input()]
res = []
for i in my_str:
    res.append(my_str.count(i))
    if my_str.count(i) == 1:
        res.append('1')
        gola = i
        break
if '1' not in res:
    print('-1')
else:
    print(i)

发表于 2022-07-03 19:08:13 回复(0)
a = input()
flag =''
for i in a:
    if a.count(i)==1:
        flag = i
        break
    else:
        flag = -1
print(flag)

发表于 2022-07-02 23:37:07 回复(0)
while True:
    try:
        string = input()
        res = []
        res_i = '-1'
        for i in range(len(string)):
            if string[i] not in res:
                res.append(string[i])
        for i in str(res):
            if string.count(i) == 1:
                res_i = i
                break
            else:
                pass
        print(res_i)
    except:
        break
发表于 2022-06-30 12:46:07 回复(0)
简单
s = input()
for i in s:
    j = s.count(i)
    if j == 1:
        print(i)
        break
else:
    print(-1)

发表于 2022-06-29 21:47:41 回复(0)
s=str(input())
l=[]
for i in s:
    x=s.count(i)
    if x==1:
        l.append(i)
if len(l)!=0:
    print(l[0])
else: print(-1)

发表于 2022-06-17 14:21:15 回复(0)
while True:
    try:
        strs = input()
        n = 0  #记录输出次数
        for i in range(len(strs)):
            if strs.count(strs[i]) == 1:
                print(strs[i])
                n = 1
                break    #只记录第一个满足要求的数
        if n == 0:   #输出次数为0,则不存在只出现一次的数
            print("-1")   
    except:
        break

发表于 2022-06-09 15:49:59 回复(0)

问题信息

难度:
73条回答 54761浏览

热门推荐

通过挑战的用户

查看代码